package org.nlp2rdf.navigator.junit.component;

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.nlp2rdf.navigator.client.common.ConceptDTO;
import org.nlp2rdf.navigator.client.common.StoredConceptDTO;
import org.nlp2rdf.navigator.client.common.RelatedConceptsDTO.StrategyDTO;
import org.nlp2rdf.navigator.component.exception.ConceptSavingFailedException;
import org.nlp2rdf.navigator.component.store.LogicalRelationStrategy;
import org.nlp2rdf.navigator.component.store.SimpleFileStore;
import org.nlp2rdf.navigator.component.store.XMLFileStore;
import org.nlp2rdf.navigator.util.LogHelper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Store {

	{
		LogHelper.touch();
	}

	ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "test/store.xml" });

	LogicalRelationStrategy lrs = (LogicalRelationStrategy) context
			.getBean("dbpedia.logicalRelationStrategy");

	XMLFileStore store = (XMLFileStore) context.getBean("dbpedia.store");

	ConceptDTO real_countries = new ConceptDTO();
	StoredConceptDTO sovereigntyType = new StoredConceptDTO();

	@Before
	public void init() {

		real_countries
				.setKbSyntaxString("(\"http://www.opengis.net/gml/_Feature\" AND EXISTS \"http://dbpedia.org/property/sovereigntyType\".TOP)");
		real_countries
				.setManchesterSyntaxString("http://www.opengis.net/gml/_Feature and http://dbpedia.org/property/sovereigntyType some Thing");

		sovereigntyType.setKbSyntaxString(" EXISTS \"http://dbpedia.org/property/sovereigntyType\".TOP");
		sovereigntyType.setLabel("hat einen sovereigntype");
		sovereigntyType
				.setManchesterSyntaxString("http://www.opengis.net/gml/_Feature and http://dbpedia.org/property/sovereigntyType some Thing");
		sovereigntyType.setUri(store.generateUriFromLabel(sovereigntyType));

	}

	@Test
	public void test() throws ConceptSavingFailedException {
		List<StrategyDTO> ls = lrs.getRelatedConcepts(real_countries);
		assertTrue(!ls.isEmpty());
		for (StrategyDTO strategyDTO : ls) {
			if (strategyDTO.getTitle().equals("super classes")) {
				assertTrue(strategyDTO.getConcepts().size() == 1);
				assertTrue(strategyDTO.getNormalConcepts().size() == 1);
				assertTrue(strategyDTO.getStoredConcepts().size() == 0);
				assertTrue(strategyDTO.getConcepts().get(0).getManchesterSyntaxString().equals(
						"http://www.opengis.net/gml/_Feature"));
			}
		}
		lrs.update(sovereigntyType);
		ls = lrs.getRelatedConcepts(real_countries);

		assertTrue(!ls.isEmpty());
		for (StrategyDTO strategyDTO : ls) {
			if (strategyDTO.getTitle().equals("super classes")) {
				assertTrue(strategyDTO.getConcepts().size() == 2);
				assertTrue(strategyDTO.getNormalConcepts().size() == 1);
				assertTrue(strategyDTO.getStoredConcepts().size() == 1);
				assertTrue(strategyDTO.getConcepts().get(0).getManchesterSyntaxString().equals(
						"http://www.opengis.net/gml/_Feature"));
			}
		}

		// System.out.println(ls);

		// dbpedia.update(sovereigntyType);

	}

}
